package net.hzlj.platform.enterprise.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.GsonBuilder;

import net.hzlj.platform.enterprise.beans.resp.RespBaseBean;
import net.hzlj.platform.enterprise.common.Constant;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;
import java.util.Set;

/**
 * @ClassName: SharedPreferencesUtils
 * @Description: 本地缓存
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-27 09:55:39
 */
public class PreferencesUtils {

    private static final String TAG = PreferencesUtils.class.getSimpleName();

    public static String PREFERENCE_NAME = Constant.PROJECT_NAME;

    //记住密码
    public static final String KEY_REMIND_PASSWORD = "key_remind_password";
    //用户信息
    public static final String KEY_USER_INFO = "key_user_info";
    //当前登录者账号
    public static final String KEY_USER_ACCOUNT = "key_user_account";
    //网络时间
    public static final String KEY_NETWORK_TIME = "key_network_time";

    public static final String KEY_USER_UPLOAD_URL = "key_user_upload_url";

    public static final String KEY_USER_UPLOAD_PATH = "key_user_upload_path";

    public static final String KEY_GE_TUI_CLIENT_ID = "key_ge_tui_client_id";

    //轮询时间
    public static final String KEY_LOCATION_INTERVAL = "key_location_interval";
    //是否允许卸载
    public static final String KEY_ALLOW_UNINSTALLATION = "key_allow_uninstallation";
    //更新问卷状态
    public static final String KEY_QUESTION_STATUS = "key_question_status";

    public static boolean putString(Context context, String key, String value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    public static boolean putBoolean(Context context, String key, boolean value) {
        Log.i(TAG,"putBoolean key:" + key + ":" + value);
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        boolean flag = settings.getBoolean(key, defaultValue);
        Log.i(TAG,"getBoolean key:" + key + ":" + flag + ",defaultValue:" + defaultValue);
        return flag;
    }

    public static boolean putInt(Context context, String key, int value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

    public static boolean putFloat(Context context, String key, float value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }

    public static boolean putLong(Context context, String key, long value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }

    @SuppressLint("NewApi")
    public static boolean putStringSet(Context context, String key, Set<String> value) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putStringSet(key, value);
        return editor.commit();
    }

    @SuppressLint("NewApi")
    public static Set<String> getStringSet(Context context, String key, Set<String> defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        return settings.getStringSet(key, defaultValue);
    }

    public static <T> boolean putObject(Context context, String key, T o) {

        /*SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        boolean flag = false;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(o);
            String str = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(key, str);
            flag = editor.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/
        if (o == null){
            return false;
        }
        return putString(context,key,JSON.toJSONString(o));
    }

    public static <T> T getObject(Context context, String key,Class<T> cla) {
        String content = getString(context,key,null);
        if (StringUtils.isNotEmpty(content)){
            return new GsonBuilder().create().fromJson(
                    content, cla);
        }else {
            return null;
        }
        /*SharedPreferences preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        String content = preferences.getString(key, "");
        if (StringUtils.isEmpty(content)){

            try {
                return (T)cla.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        byte[] base64Bytes = Base64.decode(content.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            T t = cla.newInstance();
            bais = new ByteArrayInputStream(base64Bytes);
            ois = new ObjectInputStream(bais);
            Object o = ois.readObject();
            if (o != null){
                return (T)o;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bais != null) {
                try {
                    bais.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;*/

    }
}
